home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: news.sprintlink.net!news1!news
- From: rclark@iquest.net (Robert B. Clark)
- Subject: Re: Easiest way to center a string?
- X-Nntp-Posting-Host: ind-002-236-123.iquest.net
- Message-ID: <3124a220.3624775@news.iquest.net>
- Sender: news@iquest.net (News Admin)
- Organization: IQuest Internet, Inc.
- X-Newsreader: Forte Agent .99d/16.182
- References: <4fobka$1st@parlor.hiwaay.net> <3120d412.7714326@news.iquest.net> <4ftelp$b7l@sun001.spd.dsccc.com>
- Date: Fri, 16 Feb 1996 16:21:27 GMT
-
- On 14 Feb 1996 19:54:33 GMT, jmccarty@spd.dsccc.com (Mike McCarty)
- wrote:
-
- >In article <3120d412.7714326@news.iquest.net>,
- >Robert B. Clark <rclark@iquest.net> wrote:
- >)Compute the left column by subtracting half of the string length from
- >)half of the screen width; e.g.,
- >)
- >) x=MAXCOL / 2 - strlen(str) / 2;
- >)
- >Please do =not= use this code as it stands. It may do terrible things to
- >your screen if the string does not fit into the field. Always check for
- >fits before using such techniques.
-
- Of course, bounds checking is a good idea. But since this was a
- homework assignment, I stuck to the essentials. :-)
-
- Here is something that may be a little more robust. This is wriiten for
- DOS and uses a few DOS-specific functions and structs:
-
- /* Center.c - Turbo C v3.0
- Demonstrates how to center a string on a line for any size window.
- Usage: CENTER [winleft] [wintop] [winright] [winbottom]
- Written by Robert B. Clark <rclark@iquest.net> 13 Feb 1996 */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h> /* For gotoxy(), cprintf(), window(), etc. */
- #include <string.h> /* For strlen() */
-
- #define WINWIDTH (ti.winright-ti.winleft+1)
-
- struct text_info ti; /* Used in various functions */
-
- /* Centers a string in a window.
- Uses global text_info struct ti.
- Returns -1 if computed left column is out of bounds. */
- int Center(const char *str)
- {
- int lc, len;
-
- len=strlen(str);
- gettextinfo(&ti); /* Get text-mode video info */
- lc=WINWIDTH / 2 - len / 2 + 1; /* Compute left column */
- if (len>WINWIDTH) {
- fprintf(stderr,"\n\aWidth of text window is insufficient.");
- lc=-1;
- }
- else {
- gotoxy(lc,wherey());
- #ifdef WINDOWDRESSING /* Just for fun */
- len=ti.attribute; /* Get current text attribute */
- textcolor((len & 15)+BLINK); /* Make text foreground blink */
- cputs(str);
- textcolor(len & 15); /* Reset original attribs */
- #else
- cputs(str);
- #endif
- }
- return lc;
- } /* Center */
-
-
- /* Simply prints current window x-coordinates (nominally 1..80)
- Uses global text_info struct ti. */
- void PrintXCoords(void)
- {
- int j; char c;
-
- cputs("\n\r");
- /* Print x-coordinates MSD (0,10,20,30...) */
- j=1; c='1';
- while(j<=WINWIDTH) {
- if (j / 10 * 10 == j)
- putch(c++);
- else
- putch(' ');
- if (c>'9') c='0';
- j++;
- }
- /* Print x-coordinates LSD (1,2,3...) */
- j=1; c='1';
- while(j<=WINWIDTH) {
- putch(c++);
- if (c>'9') c='0';
- j++;
- }
- } /* PrintXCoords */
-
-
- /* Set up text window */
- void SetWindow(int x1, int y1, int x2, int y2, int fore, int back)
- {
- textattr(WHITE+(BLACK<<4)); /* Reset screen to white on black */
- clrscr();
- window(x1,y1,x2,y2); /* Create new text window */
- textattr(fore+(back<<4)); /* Change window colors so it */
- clrscr(); /* will stand out */
- } /* SetWindow */
-
-
- int main(int argc, char *argv[])
- {
- const char str[]="This text is centered";
- int x,x1=10,y1=8,x2=70,y2=20;
-
- if (argc>1) { /* Get command line coordinate */
- x1=atoi(argv[1]); /* values if specified */
- if (argc>2) y1=atoi(argv[2]);
- if (argc>3) x2=atoi(argv[3]);
- if (argc>4) y2=atoi(argv[4]);
- }
- SetWindow(x1,y1,x2,y2,WHITE,GREEN);
- gettextinfo(&ti); /* Needed for WINWIDTH */
- cprintf("Requested window coordinates are (%d,%d) to (%d,%d)."
- "\n\rCurrent window coordinates are (%d,%d) to (%d,%d)."
- "\n\rString length is %d."
- "\n\rWindow width is %d.\n\n\r",
- x1,y1,x2,y2,ti.winleft,ti.wintop,ti.winright,ti.winbottom,
- strlen(str),WINWIDTH);
- x=Center(str);
- PrintXCoords();
- if (x!=-1) cprintf("\n\rCentered text at %d.",x);
- window(1,1,ti.screenwidth,ti.screenheight); /* Restore window */
- gotoxy(1,22); /* to full screen */
- return x;
- }
-
- --
- Robert B. Clark <rclark@iquest.net>
- "Be wary of strong spirits. It can make you shoot at tax collectors...
- and miss." --RAH
-